package org.gitos.cloud.comet.logic;

import org.gitos.cloud.domain.mq.MqMsg;
import org.gitos.cloud.domain.tcp.Bucket;

/**
 * Created by shizhengchu on 16/3/18.
 */
public class ContextPipline {



    private ContextLogicHandler head = null;
    private ContextLogicHandler tail = null;

    private Bucket bucket;
    private MqMsg mqMsg;

    public ContextPipline(Bucket bucket, MqMsg mqMsg) {
        this.bucket = bucket;
        this.mqMsg = mqMsg;
    }

    public ContextPipline lastAdd(LogicHandler handler) {
        ContextLogicHandler currentCtxHandler = new ContextLogicHandler(handler, this);
        if (head == null){
            NothingLogicHandler nothingLogicHandler = new NothingLogicHandler();
            head = new ContextLogicHandler(nothingLogicHandler, this);
        }
        if(tail == null){
            NothingLogicHandler nothingLogicHandler = new NothingLogicHandler();
            tail = new ContextLogicHandler(nothingLogicHandler, this);
        }
        ContextLogicHandler last = head;
        while (last.getNext() != null) {
            last = last.getNext();
        }
        last.setNext(currentCtxHandler);
        currentCtxHandler.setPre(last);

        tail.setPre(currentCtxHandler);
        currentCtxHandler.setNext(tail);


        return this;
    }

    public ContextLogicHandler getHeadHander() {
        return head;
    }

    public ContextLogicHandler getTailHander() {
        return tail;
    }

    public Bucket getBucket() {
        return bucket;
    }

    public void setBucket(Bucket bucket) {
        this.bucket = bucket;
    }

    public MqMsg getMqMsg() {
        return mqMsg;
    }

    public void setMqMsg(MqMsg mqMsg) {
        this.mqMsg = mqMsg;
    }

    static  class NothingLogicHandler implements LogicHandler{
        @Override
        public boolean tcpbound() {
            return false;
        }

        @Override
        public boolean mqbound() {
            return false;
        }

        @Override
        public void tcplogic(ContextLogicHandler ctx) {

        }

        @Override
        public void mqlogic(ContextLogicHandler ctx) {

        }
    }
}
